/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package EmployeeRESTService;

import BUS.MainController;
import Entity.Employee;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 * REST Web Service
 *
 * @author HOANG ANH
 */
@Path("employee-rest")
public class EmployeeRESTServer {

    @Context
    private UriInfo context;
    private static MainController controller;

    /** Creates a new instance of EmployeeRESTServer */
    public EmployeeRESTServer() {
        if (controller == null) {
            controller = new MainController();
        }
    }

    private String checkInput(Employee employee) {
        if (employee.getName().length() < 2) {
            return "Employee name too short";
        }
        if (employee.getCountry().length() < 2) {
            return "Employee country too short";
        }

        Calendar birthday = Calendar.getInstance();
        birthday.setTime(employee.getBirthday());
        Calendar today = Calendar.getInstance();
        int age = today.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
        if (today.get(Calendar.DAY_OF_YEAR) <= birthday.get(Calendar.DAY_OF_YEAR)) {
            age--;
        }
        if (age < 18) {
            return "Employee must 18 or older ( your age : " + age + " )";
        }
        return "pass";
    }

    /**
     * Retrieves representation of an instance of EmployeeRESTService.EmployeeRESTServer
     * @return an instance of java.lang.String
     */
    @GET
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response getXml() {
        try {
            //TODO return proper representation object
            ArrayList<Employee> list = controller.getListEmployee();
            GenericEntity gEntity = new GenericEntity<ArrayList<Employee>>(list) {
            };
            return Response.ok(gEntity).build();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return Response.status(Response.Status.BAD_REQUEST).build();
    }

    /**
     * PUT method for updating or creating an instance of EmployeeRESTServer
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @POST
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response postXml(Employee employee) {
        try {
            String check = checkInput(employee);
            if (!check.equals("pass")) {
                throw new Exception(check);
            }
            controller.Register(employee);
            return Response.ok().build();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
    
    @PUT
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public Response updateXml(Employee employee) {
        try {
            String check = checkInput(employee);
            if (!check.equals("pass")) {
                throw new Exception(check);
            }
            controller.updateEmployee(employee);
            return Response.ok().build();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
    
    @DELETE
    @Consumes(MediaType.TEXT_PLAIN)
    public Response delXml(String name){
        try {
            controller.delEmployee(name);
            return Response.ok().build();
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return Response.status(Response.Status.BAD_REQUEST).build();
    }
}
